using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace LeisureCenter.Controls
{
    public class RatingListBox : ListBox
    {
        protected override void DrawItemText(ListBoxItem item, Graphics graphics, Rectangle textBounds, Brush textBrush)
        {
            if (item is RatingListItem)
            {
                Rectangle ratingBounds = textBounds;
                ratingBounds.X = ratingBounds.Right - 110;
                textBounds.Width -= 110;
                RatingBar.DrawRating(graphics, ratingBounds, Font.Size, textBrush, (int)((RatingListItem)item).Rating);
            }
            base.DrawItemText(item, graphics, textBounds, textBrush);
        }

        protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left && SelectedItem is RatingListItem)
            {
                ((RatingListItem)SelectedItem).Rating--;
                Invalidate();
                e.Handled = true;
            }
            else
                if (e.KeyCode == Keys.Right && SelectedItem is RatingListItem)
                {
                    ((RatingListItem)SelectedItem).Rating++;
                    Invalidate();
                    e.Handled = true;
                }
                else
                    base.OnKeyDown(e);
        }
    }

    public class RatingListItem : ListBoxItem
    {
        public RatingListItem(ListBox listBox, string text, string iconName, long rating)
            : base(listBox, text, iconName)
        {
            this.rating = rating;
        }

        private long rating;

        public long Rating
        {
            get { return rating; }
            set { rating = Math.Min(Math.Max(value, 0), RatingBar.MaxRating); }
        }
    }
}
